<template>
	<view class="content">
		<view class="custom-info">
			<view class="info-banner">
				<image :src="data.cover" mode=""></image>
			</view>
			<view class="custom-info-head">
				<view class="custom-info-head-bt">
					<view class="custom-info-h4">{{data.title}}</view>
					<text>已售：{{data.sales}}</text>
				</view>
				<view class="custom-info-head-mobv">
					<view class="custom-mobv-jg">
						<view>￥{{data.discount_price}}</view>
						<text>￥{{data.market_price}}</text>
					</view>
					<cartcontrol :food="data" @add="addCart" @dec="decreaseCart"></cartcontrol>
				</view>
			</view>
			<view class="custom-info-main">
				<view class="custom-info-main-h4">商品详情</view>
				<view class="custom-info-wenben">
					<rich-text :nodes="data.describe"></rich-text>
				  <view class="wenben-list">
						<text v-for="(tiem,name,index) in data.param" :key="index">{{name}}：{{tiem}}</text>
					</view>
				</view>
			</view>
		</view>
		<shopcart :goods="GWClist" @add="addCart" @dec="decreaseCart" @delAll="delAll"></shopcart>
	</view>
</template>

<script>
	import shopcart from '@/components/shopcart.vue'
	import cartcontrol from '@/components/cartcontrol.vue'
	export default {
		components: {
			shopcart,
			cartcontrol
		},
		data() {
			return {
				data: '',
				GWClist:[],
			};
		},
		onLoad(id) {
			this.getData(id.id)
		},
		methods: {
			getData(id) { //获取详情
				var than = this;
				uni.request({
					url: getApp().globalData.http + "/made/info",
					data: {
						id: id
					},
					method: "post",
					header: {
						"content-type": "application/x-www-form-urlencoded",
						token: uni.getStorageSync("token")
					},
					success: res => {
						if (res.data.code == 1) {
							than.data = res.data.data
							console.log(than.data);
							
							// than.getGWC()
						} else {
							wx.showToast({
								title: res.data.msg,
								icon: "none",
								duration: 2000
							});
						}
					}
				});
			},
			addCart: function() { //加菜
				var item = this.data
				let goodsid = item.id
				if (item.goods_id) {
					goodsid = item.goods_id
				}
				var than = this;
				uni.request({
					url: getApp().globalData.http + "/shoppingCart/put",
					data: {
						type: 1,
						goods_id: goodsid,
						num: 1,
						remark: '',
						detail: {
							id: item.id,
							title: item.title,
							cover: item.cover,
							market_price: item.market_price,
							discount_price: item.discount_price,
						}
					},
					method: "post",
					header: {
						token: uni.getStorageSync("token")
					},
					success: res => {
						if (res.data.code == 1) {
							item.num++
							than.getGWC()
						} else {
							wx.showToast({
								title: res.data.msg,
								icon: "none",
								duration: 2000
							});
						}
					}
				});
			},
			decreaseCart() { //减菜
				var item = this.data
				var than = this;
				uni.request({
					url: getApp().globalData.http + "/shoppingCart/remove",
					data: {
						id: item.idNum,
						num: 1,
					},
					method: "post",
					header: {
						"content-type": "application/x-www-form-urlencoded",
						token: uni.getStorageSync("token")
					},
					success: res => {
						if (res.data.code == 1) {
							if (item.num) {
								item.num--
								than.getGWC()
							}
						} else {
							wx.showToast({
								title: res.data.msg,
								icon: "none",
								duration: 2000
							});
						}
					}
				});
			},
			getGWC() { //获取购物车列表    
				var than = this
				uni.request({
					url: getApp().globalData.http + "/shoppingCart/getList",
					data: {},
					method: "post",
					header: {
						"content-type": "application/x-www-form-urlencoded",
						token: uni.getStorageSync("token")
					},
					success: res => {
						than.GWClist = res.data.data
						than.GWClist.forEach(e => {
							e.idNum = e.id
							if (e.goods_id == than.data.id) {
								than.data.num = e.num
								than.data.idNum = e.id
							}
						});
					}
				});
			},
			delAll() { // 清空购物车
				var than = this;
				uni.request({
					url: getApp().globalData.http + "/shoppingCart/clear",
					data: {},
					method: "post",
					header: {
						"content-type": "application/x-www-form-urlencoded",
						token: uni.getStorageSync("token")
					},
					success: res => {
						if (res.data.code == 1) {
							than.GWClist = []
							than.data.num = 0
						} else {
							wx.showToast({
								title: res.data.msg,
								icon: "none",
								duration: 2000
							});
						}
					}
				});
			},
		}
	}
</script>